package com.example.demo.controller;

import org.apache.commons.lang3.RandomUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/event")
public class EventStreamController {
    @GetMapping(value = "/test",produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public void test(HttpServletResponse response, HttpServletRequest request) throws IOException, InterruptedException {
        PrintWriter writer = response.getWriter();
        for(int i=3;i>0; i--) {
            writer.write("<br/>AAAA"+ RandomUtils.nextInt());
            response.flushBuffer();
            TimeUnit.SECONDS.sleep(3);
        }

    }
}
